import * as constants from './constants';
import {reqLogin} from '../../api/index'
import store from  '../../utils/storageUtils'
import {
    message
} from 'antd'

const loginsuccess = (userinfo) => ({
        type:constants.USER_LOGIN,
        userinfo:userinfo
});

const  out =()=>({
    type:constants.USER_OUT
})

export  const  login=(user)=>{
    return async (dispatch)=>{
        const result = await reqLogin(user.username,user. password)
 console.log('login()', result)
        if(result.status === 0) {
            if (user.remember) {
            store.saveUserstora(result.data)
            }else {
                store.saveUserseesion(result.data)
            }
            dispatch(loginsuccess(result.data))
            message.success('登录成功', 2)

    }else {
            message.error(result.msg)
        }
    }
}

export  const  loginout=()=>{
    return (dispatch)=>{
        dispatch({
            type:constants.USER_OUT
        })
    }
}
